import { toolsHttp } from '/@/utils/http';
import type {
  CardList,
  CardQueryParams,
  CardInfoData,
  CardDetailParams,
  GameId,
  GameConfig,
  SearchConfigList,
  CardAssociativeWordParams,
  CardAssociativeWordData,
  CardSearchHistoryData,
  CardSearchHistoryDelete,
} from './model/cardModel';
import type { ErrorMessageMode } from '/#/axios';
import { isArray } from '/@/utils/is';
import { includes } from 'lodash-es';
function InitParams(params: CardQueryParams) {
  const jsonParams: any = {};
  for (const item in params) {
    if (isArray(params[item])) {
      jsonParams[item] = params[item].length ? params[item].join(',') : undefined;
      continue;
    }
    jsonParams[item] = params[item];
  }
  return jsonParams;
}

enum Api {
  CardGameConfig = '/app/game/get-config',
  CardSearchConfig = '/app/card/get-search-config',
  CardList = '/app/card/get-card-list',
  DeckCardList = '/app/card-deck/get-card-list',
  CardDetail = '/app/card/get-card-info',
  CardAssociativeWord = '/app/search/get-associative-word',
  CardSearchHistory = '/app/search/get-history',
  CardDeleteHistory = '/app/search/delete-history',
}

// 获取游戏配置
export function CardGameConfigApi(params: GameId, model: ErrorMessageMode = 'dialog') {
  return toolsHttp.post<GameConfig>(
    {
      url: Api.CardGameConfig,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取搜索配置
export function CardSearchConfigApi(params: GameId, model: ErrorMessageMode = 'dialog') {
  return toolsHttp.post<SearchConfigList>(
    {
      url: Api.CardSearchConfig,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 查询卡牌列表 isDeck: 是否是组牌调用
export function CardListApi(params: CardQueryParams, model: ErrorMessageMode = 'dialog', isDeck = false) {
  const jsonParams = InitParams(params);
  const searchFormJson: any = {};
  const excludeKeyArr = ['game_id', 'page', 'size', 'name', 'series_ids'];
  for (const ketStr in jsonParams) {
    if (!includes(excludeKeyArr, ketStr)) {
      searchFormJson[ketStr] = jsonParams[ketStr];
    }
  }
  jsonParams['search_form_json'] = JSON.stringify(searchFormJson);
  let url = Api.CardList;
  if (isDeck) {
    jsonParams['is_deck'] = 1;
    url = Api.DeckCardList;
  }
  return toolsHttp.post<CardList>(
    {
      url: url,
      data: jsonParams,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取卡牌详情
export function CardDetailApi(params: CardDetailParams, model: ErrorMessageMode = 'dialog') {
  return toolsHttp.post<CardInfoData>(
    {
      url: Api.CardDetail,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取联想词
export function CardAssociativeWordApi(params: CardAssociativeWordParams, model: ErrorMessageMode = 'dialog') {
  return toolsHttp.post<CardAssociativeWordData>(
    {
      url: Api.CardAssociativeWord,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取历史记录
export function CardSearchHistoryApi(params: GameId, model: ErrorMessageMode = 'dialog') {
  return toolsHttp.post<CardSearchHistoryData>(
    {
      url: Api.CardSearchHistory,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 删除历史记录
export function CardDeleteHistoryApi(params: CardSearchHistoryDelete, model: ErrorMessageMode = 'dialog') {
  return toolsHttp.post(
    {
      url: Api.CardDeleteHistory,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}
